//
/**
 * Filename: fig10-26_ed.c
 * Author:   csbonkers
 * Contact:  forever65535@qq.com
 * Date:     2021/6/20
 * Description:
 *    
 */
#include "apue.h"
#include <sys/wait.h>
#include <errno.h>
static int c_system(const char *cmdstring) { /* version without signal handling */
  if (cmdstring == NULL) {
    return -1;
  }
  int status;
  pid_t pid;
  if ((pid = fork()) < 0) {
    status = -1; // probably out of process
  } else if (pid == 0) {
    execl("/bin/sh", "sh", "-c", cmdstring, NULL);
    _exit(127); // execl error
  } else {
    while (waitpid(pid, &status, 0) < 0) {
      if (errno == EINTR) {
        status = -1;
        break;
      }
    }
  }
  return status;
}

static void sig_int(int signo){
  printf("caught SIGINT\n");
}
static void sig_chld(int signo){
  printf("caught SIGCHLD\n");
}

int main(){
  if(c_signal(SIGINT, sig_int) == SIG_ERR){
    printf("signal(SIGINT) error");
  }
  if(c_signal(SIGCHLD, sig_chld) == SIG_ERR){
    printf("signal(SIGCHLD) error");
  }
  if(c_system("ed") < 0){
    err_sys("system() error");
  }
  return 0;
}
